package org.example.gradleauth.model.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import gradle.common.core.constants.CommonConstant;
import gradle.common.core.mode.entity.SystemUser;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 登录后台员工的用户
 *
 * @author 1141193930@qq.com
 */
@Data
public class StaffUserDetails implements UserDetails {

    private  Long id;

    private  String username;

    private  String phone;

    private  String email;

    @JsonIgnore
    private  String password;

    private  Integer enable;

    private List<GrantedAuthority> authorities;

    public StaffUserDetails(SystemUser customUser, List<GrantedAuthority> authorities) {
        this.id = customUser.getId();
        this.password = customUser.getPassword();
        this.username = customUser.getUsername();
        this.phone = customUser.getEmployeePhone();
        this.email = customUser.getEmployeeEmail();
        this.enable = customUser.getEnable();
        this.authorities = authorities;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return Objects.equals(getEnable(), CommonConstant.ENABLE);
    }


}
